package com.weixin.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.weixin.bean.Orders;
import com.weixin.service.DinnerTableService;
import com.weixin.service.OrdersService;
import com.weixin.service.impl.DinnerTableServiceimpl;
import com.weixin.service.impl.OrdersServiceimpl;

@WebServlet("/OrdersServlet/*")
public class OrdersServlet extends HttpServlet {

	private OrdersService ordersService = new OrdersServiceimpl();
	
	private DinnerTableService dinnerTableService = new DinnerTableServiceimpl();

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String pathInfo = request.getPathInfo().substring(1);
		if (pathInfo.equals("addOrder")) {
			addOrder(request, response);
		} else if (pathInfo.equals("listAllOrders")) {
			listAllOrders(request, response);
		} else if (pathInfo.equals("jiezhang")) {
			jiezhang(request, response);
		} else if (pathInfo.equals("removeByIdJson")) {
			removeByIdJson(request, response);
		}
	}

	private void removeByIdJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));
		if (ordersService.delete(id)) {
			response.getWriter().print(1);
		} else {
			response.getWriter().print(0);
		}
	}

	private void jiezhang(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int orderId = Integer.valueOf(request.getParameter("orderId"));
		int orderStatus = 1;
		boolean b = ordersService.update(orderId, orderStatus);

		if (b) {
			response.getWriter().print(1);
		} else {
			response.getWriter().print(0);
		}
	}

	private void listAllOrders(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		//response.setContentType("text/javascript; charset=utf-8");
		String sousuo = request.getParameter("search");
		String search="";
		if(sousuo.equals("玫瑰")) {
			search="1";
		}else if(sousuo.equals("牡丹")) {
			search="2";
		}else if(sousuo.equals("荷花")) {
			search="3";
		}else if(sousuo.equals("荷花")) {
			search="4";
		}else {
			search=sousuo;
		}

		// 接收分页参数：
		// 起始索引
		int offset = Integer.valueOf(request.getParameter("offset"));
		// 每页大小
		int limit = Integer.valueOf(request.getParameter("limit"));

		// 服务器端分页：
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		List<Orders> ordersList = ordersService.getAll(offset, limit, search);
		int total=ordersService.selectCount();
		Map map = new HashMap();
		map.put("total", total);
		map.put("rows", ordersList);
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		response.getWriter().print(gson.toJson(map));
	}

	private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		Orders orders = new Orders();
		double totalPrice = Double.valueOf(request.getParameter("totalPrice"));
		orders.setTotalPrice(totalPrice);
		Date orderDate = new Date();
		orders.setOrderDate(orderDate);

		orders.setTableId(Integer.parseInt(request.getParameter("zhuohao")));
		int tableStatus=1;
		boolean c=dinnerTableService.update(Integer.parseInt(request.getParameter("zhuohao")), tableStatus, orderDate);

		boolean b = ordersService.addOrders(orders);
		if (b) {
			response.getWriter().print(1);
		} else {
			response.getWriter().print(0);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
